package com.weirong.smartvault.backservice.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.weirong.smartvault.backservice.controller.fragment_entity.PagesIndex;
import com.weirong.smartvault.backservice.service.ConfirmLogService;
import com.weirong.smartvault.common.tools.constant.ThymleafVarName;
import com.weirong.smartvault.backservice.util.table.ConfirmTable;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.ConfirmDetailLogRepository;
import com.weirong.smartvault.dao.ConfirmLogRepository;
import com.weirong.smartvault.entity.ConfirmDetailLog;
import com.weirong.smartvault.entity.ConfirmLog;
import com.weirong.smartvault.entity.Department;
import com.weirong.smartvault.backservice.entity_model.ConfirmLogModel;
import com.weirong.smartvault.backservice.entity_model.DepartmentModel;
import com.weirong.smartvault.backservice.user.IUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kfeb4
 * Date: 2017/12/25
 * Time: 10:29
 */

@Controller
@RequestMapping(value = "/confirm")
public class Confirm {
    @Autowired
    ConfirmLogRepository confirmLogRepository;

    @Autowired
    ConfirmLogService confirmLogService;

    @Autowired
    ConfirmDetailLogRepository confirmDetailLogRepository;

    @RequestMapping(value = "/confirm", method = RequestMethod.GET)
    public String confirmGet(ModelMap model) throws ParseException, NoSuchMethodException, NoSuchFieldException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //核账
        IUser user = IUser.getNowUser();
        ConfirmLog confirmLog = user.getNowConfirmLog();
        if (confirmLog != null) {//有可以核账的内容
            ConfirmLogModel confirmLogModel = new ConfirmLogModel(confirmLog);
            model.addAttribute(ThymleafVarName.Var.table, ConfirmTable.buildTable(confirmLogModel));
        }
        return "confirm/confirm";
    }

    @RequestMapping(value = "/confirm_result")
    public String confirmResultGet(ModelMap model, @RequestParam(value = "page", defaultValue = "0") Integer page,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size, @RequestParam(value = "date", defaultValue = "") String date) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ParseException, InstantiationException {
        Date dt = DateUtil.getTodayOrParse(date);
        IUser user = IUser.getNowUser();
        Department d = user.getDepartment();
        DepartmentModel department = new DepartmentModel(d);
        List<DepartmentModel> departmentModels = department.getAllDepartments();
        Page confirmLogs = confirmLogService.findByBranchIdsAndDateWithPaging(ListTool.map(departmentModels, DepartmentModel::getId), dt, page, size);

        model.addAttribute(ThymleafVarName.Var.confirmLogs, ListTool.cast(confirmLogs, ConfirmLogModel.class));

        model.addAttribute(ThymleafVarName.pages, new PagesIndex(page, confirmLogs.getTotalPages(),"date",date));

        return "confirm/confirm_result";
    }

    @RequestMapping(value = "/confirm_result_detail", method = RequestMethod.GET)
    public String confirmGet(ModelMap model, @RequestParam(value = "id", defaultValue = "-1") Integer id) throws NoSuchMethodException, NoSuchFieldException, InstantiationException, IllegalAccessException, InvocationTargetException {
        ConfirmLog confirmLog = null;
        if (id > 0)
            confirmLog = confirmLogRepository.findOne(id);
        if (confirmLog != null) {//有可以核账的内容
            ConfirmLogModel confirmLogModel = new ConfirmLogModel(confirmLog);
            model.addAttribute(ThymleafVarName.Var.table, ConfirmTable.buildTable(confirmLogModel));
        }
        return "/confirm/confirm_result_detail";
    }

    @RequestMapping(value = "save_confirm", method = RequestMethod.POST)
    @ResponseBody
    public Object saveConfirm(@RequestBody String info) {
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        List<JsonObject> data = gson.fromJson(info, type);
        for (JsonObject dt : data) {
            Integer id = dt.get("id").getAsInt();
            Boolean check = dt.get("checked").getAsBoolean();
            String reson = dt.get("reason").getAsString();
            ConfirmDetailLog confirmDetailLog= confirmDetailLogRepository.findOne(id);
            confirmDetailLog.setConfirmResult(check?"success":"fail");
            confirmDetailLog.setFailReason(reson);
            confirmDetailLogRepository.save(confirmDetailLog);
        }
        return null;
    }
}
